package sqlanalysisgui.editor;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.zest.core.viewers.GraphViewer;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.core.widgets.ZestStyles;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.HorizontalTreeLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;

import sqlanalysisgui.session.SessionInfo;
import sqlanalysisgui.view.model.TableInfo;

import com.cho.core.db.dao.TableDao;
import com.cho.core.vo.TableVo;

public class DashboardModeling extends EditorPart {

	public static final String ID = "sqlanalysisgui.editor.DashboardModeling"; //$NON-NLS-1$
	private Logger logger = Logger.getLogger(this.getClass());
	private DashboardTableInput input;
	private Graph graph;
	private int layout = 1;

	private GraphViewer viewer;
	
	public DashboardModeling() {
	}

	/**
	 * Create contents of the editor part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
	/*	Shell shell = parent.getShell();
		shell.setText("GraphSnippet1");
		shell.setLayout(new FillLayout());
		shell.setSize(700, 400);
		*/
		
//		System.out.println("parent.getSize(): " + parent.getSize());
//		System.out.println("parent.getSize(): " + parent.getSize().x);
//		System.out.println("parent.getSize(): " + parent.getSize().y);
	//	parent.setSize(700, 2000);
//		System.out.println("parent.getSize(): " + parent.getSize());
//		System.out.println("parent.getSize(): " + parent.getSize().x);
//		System.out.println("parent.getSize(): " + parent.getSize().y);
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		//composite.setSize(400, 400);
		//Graph g = new Graph(parent.getShell(), SWT.NONE);
		Graph g = new Graph(composite, SWT.NONE);
		
		TableDao dao = new TableDao();
		List<Map<String, String>> tableList = dao.selectTableList(new TableVo(SessionInfo.getProjectInfo().getProjectName()));
		List<TableInfo> tableInfos = null;
		TableInfo tableInfo = null;
		GraphNode crudTypeNode = null;
		GraphNode crudAndFileNode = null;
		GraphNode crudAndFileAndSourceLineNode = null;
		String beforeFileName = "";
		int tableListSize = tableList.size();
		int noteTotalCount = 0;
		
		String[] crudName = new String[]{"C","R","U","D"};
		String[] crudDmlName = new String[]{"INSERT","SELECT","UPDATE","DELETE"};
		
		for (int i = 0; i < tableListSize; i++) {
			GraphNode tableNode = new GraphNode(g, SWT.NONE, tableList.get(i).get("TABLE_NAME_AND_COMMENT"));
			 
			for (int crudIndex = 0; crudIndex < crudDmlName.length; crudIndex++) {
				beforeFileName = "";
				tableInfos = dao.selectFileAndTableList(new TableVo(SessionInfo.getProjectInfo().getProjectName(), tableList.get(i).get("TABLE_NAME"), crudName[crudIndex], "B.FILE_SEQ, "));
				for (int j = 0; j < tableInfos.size(); j++) {
					
					if(0 == j){
						//CRUD NODE 생성
						crudTypeNode = new GraphNode(g, SWT.NONE, crudDmlName[crudIndex]);
						new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, tableNode, crudTypeNode);
					}
					
					tableInfo = tableInfos.get(j);
					
					if(!beforeFileName.equals(tableInfo.getFileName())){
						//FILE NODE 생성
						crudAndFileNode = new GraphNode(g, SWT.NONE, tableInfo.getFileName());
						new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, crudTypeNode, crudAndFileNode);
					}
					
					crudAndFileAndSourceLineNode = new GraphNode(g, SWT.NONE, tableInfo.getSourceLine() + " line", tableInfo);
					logger.debug("확인: " + crudAndFileAndSourceLineNode.getData());
/*					crudAndFileAndSourceLineNode.getNodeFigure().addMouseListener(new MouseListener() {
						
						@Override
						public void mouseReleased(MouseEvent me) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void mousePressed(MouseEvent me) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void mouseDoubleClicked(MouseEvent me) {
							// TODO Auto-generated method stub
							logger.debug("노드 더블 클릭: " + me);
							
						}
					});*/
					new GraphConnection(g, ZestStyles.CONNECTIONS_DIRECTED, crudAndFileNode, crudAndFileAndSourceLineNode);
					noteTotalCount++;
					
					beforeFileName = tableInfo.getFileName();
				}
			} 
			
		}
		//System.out.println("crudAndFileAndSourceLineNode: "+crudAndFileAndSourceLineNode.getData());
		
		g.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				logger.debug("ZEST 노드 선택: " + e);
				logger.debug("ZEST 노드 선택: " + e.getSource());
				logger.debug("ZEST 노드 선택: " + e.data);
				logger.debug("ZEST 노드 선택: " + e.item);
				
				System.out.println(((Graph) e.widget).getSelection());
				System.out.println(((Graph) e.widget).getData());
				System.out.println(e.widget.getData());
				
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		logger.debug("noteTotalCount: " + noteTotalCount);
		//2000 + 
		parent.setSize(700, (noteTotalCount * 35));// HorizontalTree
		//parent.setSize((noteTotalCount * 100) , 600 );//vertical tree
		
		g.setLayoutAlgorithm(new HorizontalTreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
		
//		GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
//		GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
//		GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
//		new GraphNode(g, SWT.NONE, "Scissors");
//		new GraphNode(g, SWT.NONE, "Scissors");
//		new GraphNode(g, SWT.NONE, "Scissors");
//		new GraphNode(g, SWT.NONE, "Scissors");
//		new GraphNode(g, SWT.NONE, "Scissors");
//		g.setLayout(new FillLayout(SWT.HORIZONTAL));
//		new GraphConnection(g, SWT.NONE, n, n2);
//		new GraphConnection(g, SWT.NONE, n2, n3);
//		new GraphConnection(g, SWT.NONE, new GraphNode(g, SWT.NONE, "111"), new GraphNode(g, SWT.NONE, "2222"));
//		new GraphConnection(g, SWT.NONE, new GraphNode(g, SWT.NONE, "111"), new GraphNode(g, SWT.NONE, "2222"));
//		new GraphConnection(g, SWT.NONE, new GraphNode(g, SWT.NONE, "111"), new GraphNode(g, SWT.NONE, "2222"));
//		new GraphConnection(g, SWT.NONE, new GraphNode(g, SWT.NONE, "111"), new GraphNode(g, SWT.NONE, "2222"));
		//new GraphConnection(g, SWT.NONE, n3, n);
				
		
		
		
		
		//parent.setSize(400, 400);
//		parent.setSize(size);
	//	parent.setLayout(new FillLayout(SWT.HORIZONTAL));
		
//		Composite composite = new Composite(parent, SWT.NONE);

		// Graph will hold all other objects
/*		graph = new Graph(parent, SWT.NONE);
		// Now a few nodes
		GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
		GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
		GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
		GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");

		// Lets have a directed connection
		new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1,
				node2);
		// Lets have a dotted graph connection
		new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
		// Standard connection
		new GraphConnection(graph, SWT.NONE, node3, node1);
		// Change line color and line width
		GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE,
				node1, node4);
		graphConnection.changeLineColor(parent.getDisplay().getSystemColor(
				SWT.COLOR_GREEN));
		// Also set a text
		graphConnection.setText("This is a text");
		graphConnection.setHighlightColor(parent.getDisplay().getSystemColor(
				SWT.COLOR_RED));
		graphConnection.setLineWidth(3);

//		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( LayoutStyles.ENFORCE_BOUNDS), true);
		// Selection listener on graphConnect or GraphNode is not supported
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=236528
		graph.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println(e);
			}

		});*/

		/*
		 * Graph g = new Graph(parent, SWT.NONE); GraphNode n = new GraphNode(g,
		 * SWT.NONE, "Paper"); GraphNode n2 = new GraphNode(g, SWT.NONE,
		 * "Rock"); GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors"); new
		 * GraphNode(g, SWT.NONE, "Scissors"); new GraphNode(g, SWT.NONE,
		 * "Scissors"); new GraphNode(g, SWT.NONE, "Scissors"); new GraphNode(g,
		 * SWT.NONE, "Scissors"); new GraphNode(g, SWT.NONE, "Scissors");
		 * g.setLayout(new FillLayout(SWT.HORIZONTAL)); new GraphConnection(g,
		 * SWT.NONE, n, n2); new GraphConnection(g, SWT.NONE, n2, n3); //new
		 * GraphConnection(g, SWT.NONE, n3, n); g.setLayoutAlgorithm(new
		 * SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
		 */

		/*
		 * shell.open(); while (!shell.isDisposed()) { while
		 * (!d.readAndDispatch()) { d.sleep(); } }
		 */
	}

	public void setLayoutManager() {
		switch (layout) {
		case 1:
			graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(
					LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
			layout++;
			break;
		case 2:
			graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(
					LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
			layout = 1;
			break;

		}

	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// Do the Save operation
	}

	@Override
	public void doSaveAs() {
		// Do the Save As operation
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		// Initialize the editor part

		logger.debug("site: " + site);
		logger.debug("site.getId: " + site.getId());
		logger.debug("input: " + input);

		if (!(input instanceof DashboardTableInput)) {
			throw new RuntimeException("Wrong input");
		}

		this.input = (DashboardTableInput) input;
		setSite(site);
		setInput(input);
		// proejctInfo =
		// MyModel.getInstance().getPersonById(this.input.getId());
		// setPartName("Person ID: " + proejctInfo.getId());
		setPartName("DASH BOARD");

	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
}
